#include<iostream>
#include<cmath>
#include<unordered_map>
using namespace std;
int main()
{
	int T = 0;
	cin >> T;
	unordered_map<int, int> hash;
	for (int i = 0;i < 32;i++)
	{
		hash[pow(2, i)] = i;
	}
	while (T--)
	{
		int x, y;
		cin >> x >> y;
		int ret = 0;
		while (y--)
		{
			if (((x >> 0) & 1) == 0)
			{
				ret++;
				x++;
				continue;
			}
			else
			{
				int n = x;
				n = ~n;
				n = n & (-n);
				ret += hash[n] + 1;
				x++;
			}
		}
		cout << ret << endl;
	}
	return 0;
}


